package ie.dkit.semanticPhotoWeb.server.clientServerConnector;

import ie.dkit.semanticPhotoWeb.server.persistence.PersistenceHandler;
import ie.dkit.semanticPhotoWeb.server.persistence.storageObjects.Person;
import ie.dkit.semanticPhotoWeb.server.semanticPhotoWebServer.SemanticPhotoWebCoreServer;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Sebastian Ruehl
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
public class RDFService extends HttpServlet {

	private final static Logger logger = Logger.getLogger(RDFService.class.getName());

	/**
	 * 
	 */
	private static final long serialVersionUID = 6545024553174616137L;

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String requestString = req.getParameter("all");
		if (requestString != null) {
			OutputStreamWriter outputStreamWriter = new OutputStreamWriter(resp
					.getOutputStream());
			FOAF foaf = new FOAF();

			PersistenceHandler persistenceHandler = SemanticPhotoWebCoreServer
					.getInstance().getPersistenceHandler();
			for (Person p : persistenceHandler.getAllPerson()) {
				logger.info("Consuming Person" + p.getName());
				foaf.consumePerson(p);
			}
			outputStreamWriter.write(foaf.toString());
			outputStreamWriter.flush();
			return;
		}
		requestString = req.getParameter("profileID");
		if (requestString == null) {
			String protocol = req.getProtocol();
			String msg = "Please define a user profile id by profileID";
			if (protocol.endsWith("1.1"))
				resp.sendError(405, msg);
			else
				resp.sendError(400, msg);
			return;
		} else {
			OutputStreamWriter outputStreamWriter = new OutputStreamWriter(resp
					.getOutputStream());
			FOAF foaf = new FOAF();

			PersistenceHandler persistenceHandler = SemanticPhotoWebCoreServer
					.getInstance().getPersistenceHandler();

			Person person;
			try {
				person = persistenceHandler.getPersonByID(Integer
						.parseInt(requestString));
				logger.info("Person Found: " + person.getName());
			} catch (NumberFormatException e) {
				String protocol = req.getProtocol();
				String msg = "Invalid Id";
				if (protocol.endsWith("1.1"))
					resp.sendError(405, msg);
				else
					resp.sendError(400, msg);
				return;
			} catch (Exception e) {
				String protocol = req.getProtocol();
				String msg = "Person not Found";
				if (protocol.endsWith("1.1"))
					resp.sendError(405, msg);
				else
					resp.sendError(400, msg);
				return;
			}
			logger.info("Consuming Person");
			foaf.consumePerson(person);
			outputStreamWriter.write(foaf.toString());
			outputStreamWriter.flush();
			return;
		}
	}
}
